import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../featureone/home_logic.dart' as one;
import 'home_logic.dart';

class homePage extends StatelessWidget {
  final homeLogic logic = Get.put(homeLogic()); //note 不同包下同类型会报错
  final one.oneLogic oneLogic = Get.find();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Row(
        children: [
          Expanded(
            child: Wrap(
              /*少量item*/
              children: [
                Transform(
                  transform: Matrix4.rotationZ(0.3),
                  child: TextButton(
                      onPressed: () {
                        oneLogic.increase();
                      },
                      child: Obx(() => Text(
                            '${logic.msg.value} 点击了${oneLogic.count}次',
                          ))),
                ),
              ],
              // itemExtent /*滑动方向的size,固定提升性能*/ : 240,
            ),
          ),
          /*Expanded(
            child: ListView(
              children:
              //List.generate()
              logic.datas
                  .map((e) => ListTile(
                        title: Text(e.title + '${logic.datas.indexOf(e)}'),
                        leading: Icon(e.iconData),
                      ))
                  .toList(),
            ),
          ),*/
          Expanded(
            child: GetBuilder<homeLogic>(
              builder: (controller) => NotificationListener<ScrollNotification>(
                // 添加 NotificationListener 作为父容器
                onNotification: (notification) =>
                    controller.onNotification(notification),
                child: ListView.builder(
                  // ListView.separated(
                  /*separatorBuilder*/ /*也可合并入item里*/ /*: (context, index) => index % 2 == 0
                ? Divider(
                    color: Colors.green,
                  )
                : Divider(
                    color: Colors.red,
                  ),*/
                  controller: controller.controllerScroll,
                  itemBuilder: (context, index) => _Item(index),
                  itemCount: controller.datas.length,
/*某些情况代替Expanded
                shrinkWrap: true,
                physics: ClampingScrollPhysics(),*/
                ),
              ),
            ),
          ),
          Expanded(
            child: CustomScrollView(slivers: <Widget>[
              SliverAppBar(
                // 作为头图控件
                title: Text('CustomScrollView Demo'), // 标题
                floating: true, // 设置悬浮样式
                flexibleSpace: Image.network(
                    "https://media-cdn.tripadvisor.com/media/photo-s/13/98/8f/c2/great-wall-hiking-tours.jpg",
                    fit: BoxFit.cover), // 设置悬浮头图背景
                expandedHeight: 280, // 头图控件高度
              ),
              SliverList(
                //SliverList 作为列表控件
                delegate: SliverChildBuilderDelegate(
                  (context, index) => _Item(index), // 列表项创建方法
                  childCount: logic.datas.length,
                ),
              ),
            ]),
          ),
        ],
      ),
    );
  }

  GetBuilder<homeLogic> _Item(int index) {
    return GetBuilder /*实现/优化局部刷新*/ <homeLogic>(
      /* GetBuilder */ /*实现局部刷新*/ /* <ItemLogic>(
                  init: ItemLogic(controller.datas[index]),*/
      builder: (controller) => ListTile(
        title: Text(controller.datas[index].title),
        leading: Icon(controller.datas[index].iconData),
        onLongPress: controller.onlongpress(index),
        onTap: controller.onTap(index),
      ),
    );
  }
}
